package com.onlyphoto.onlymall.mvp.view.activity

import android.content.Intent
import android.os.Build
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseActivity
import com.onlyphoto.onlymall.bean.request.ConfirmReceiveBean
import com.onlyphoto.onlymall.bean.response.OrderDetailsBean
import com.onlyphoto.onlymall.bean.response.OrderDetailsHeaderBean
import com.onlyphoto.onlymall.mvp.contract.COrderDetails
import com.onlyphoto.onlymall.mvp.presenter.POrderDetailsImpl
import com.onlyphoto.onlymall.mvp.view.binder.OrderDetailsFooterBinder
import com.onlyphoto.onlymall.mvp.view.binder.OrderDetailsGoodsBinder
import com.onlyphoto.onlymall.mvp.view.binder.OrderDetailsHeaderBinder
import com.onlyphoto.onlymall.utils.UserUtil
import com.onlyphoto.onlymall.widget.AlertDialog
import kotlinx.android.synthetic.main.activity_orderdetails.*

import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter
import qiu.niorgai.StatusBarCompat


class OrderDetailsActivity : BaseActivity<POrderDetailsImpl>(), COrderDetails.IVOrderDetails, View.OnClickListener {


    var layoutManager: LinearLayoutManager? = null
    var adapter: MultiTypeAdapter? = null
    var items: Items? = null
    var orderId = 0
    var bean: OrderDetailsBean? = null
    var isReceive = false
    var refundType = 1

    override fun retry() {}

    override fun init() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(mContext, R.color.theme_color))
        }

        orderId = intent.getIntExtra("orderId", 0)
        layoutManager = LinearLayoutManager(this)
        layoutManager?.orientation = RecyclerView.VERTICAL
        items = Items()
        adapter = MultiTypeAdapter(items!!)
        adapter?.register(OrderDetailsHeaderBean::class.java, OrderDetailsHeaderBinder())
        adapter?.register(OrderDetailsBean.GoodsInfo::class.java, OrderDetailsGoodsBinder())
        adapter?.register(OrderDetailsBean::class.java, OrderDetailsFooterBinder())
        rv_details.adapter = adapter
        rv_details.layoutManager = layoutManager
    }

    override fun initListener() {
        //点击事件初始化
        refund.setOnClickListener(this)
        pay_or_receive.setOnClickListener(this)
        back.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.refund -> {
                if (bean != null) {
                    startActivity(Intent(this, RefundActivity::class.java)
                            .putExtra("orderId", bean?.order_id)
                            .putExtra("orderPrice", bean?.pay_price)
                            .putExtra("refundType", refundType))
                }
            }
            R.id.pay_or_receive -> {
                if (isReceive) {
                    AlertDialog(this).builder()
                            .setTitle("是否确认收货？")
                            .setCanceledOnTouchOutside(false)
                            .setNegativeButton("取消") {}
                            .setPositiveButton("确定") {
                                mPresenter?.confirmReceive()
                            }.show()
                } else {
                    startActivity(Intent(this, PayMethodActivity::class.java)
                            .putExtra("orderId", bean?.order_id)
                            .putExtra("payPrice", bean?.pay_price))
                }
            }
            R.id.back -> finish()
        }
    }

    override fun loadData() {
        //加载数据
        mPresenter?.getOrderDetails(2, orderId)
    }

    override fun setConfirmReceiveBean(): ConfirmReceiveBean {
        var confirmReceiveBean = ConfirmReceiveBean()
        confirmReceiveBean.order_id = bean!!.order_id
        return confirmReceiveBean
    }

    override fun getOrderDetailsSuccess(bean: OrderDetailsBean) {
        items?.clear()
        this.bean = bean
        val headerBean = OrderDetailsHeaderBean()
        headerBean.state = bean.state
        headerBean.stateExtra = bean.state_extra
        headerBean.expressCompanyPy = bean.express_company_py
        headerBean.expressNum = bean.express_num
        headerBean.name = bean.user_address_info.name
        headerBean.phone = bean.user_address_info.phone
        headerBean.address = bean.user_address_info.address
        items?.add(headerBean)
        for (goodsInfo in bean.goods_info){
            items?.add(goodsInfo)
            if (goodsInfo.gift_set == 1) {
                val gift = goodsInfo.gift_arr!!
                val giftGoods = OrderDetailsBean.GoodsInfo(gift.goods_id, 0, gift.title, gift.buy_num, 0, 0.0, gift.front_img, gift.style, gift.size, gift.color, 0, 0)
                items?.add(giftGoods)
            }
        }
        items?.add(bean)
        adapter?.notifyDataSetChanged()

        if (bean.state == 1 && bean.state_extra == 0) {
            pay_or_receive.visibility = View.VISIBLE
        } else if (bean.state == 3) {
            refund.visibility = View.VISIBLE
            pay_or_receive.visibility = View.VISIBLE
            pay_or_receive.text = "确认收货"
            isReceive = true
        } else if (bean.state == 2 || bean.state == 5){
            refund.visibility = View.VISIBLE
            pay_or_receive.visibility = View.GONE
        } else if (bean.state_extra == 10 || bean.state_extra == 11) {
            refund.visibility = View.VISIBLE
            refund.text = "修改售后"
            refundType = 2
        } else if (bean.state_extra == 12 || bean.state_extra == 13) {
            refund.visibility = View.VISIBLE
            refund.text = "查看售后"
            refundType = 3
        }

        if (bean.goods_info[0].allow_user_img == 1) {
            mPresenter?.getUploadedPic(bean.order_id, bean.goods_info[0].goods_id.toString())
        }
    }

    override fun confirmReceiveSuccess() {
        mPresenter?.getOrderDetails(2, orderId)
    }

    override fun noUploadedPic() {
        (items?.last() as OrderDetailsBean).uploadedList = ArrayList()
        adapter?.notifyItemChanged(items!!.lastIndex, "uploaded")
    }

    override fun getUploadedPicSuccess(list: ArrayList<String>) {
        (items?.last() as OrderDetailsBean).uploadedList = list
        adapter?.notifyItemChanged(items!!.lastIndex, "uploaded")
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }

    override fun isFullScreen(): Boolean {
        //是否沉浸式， 默认非沉浸式
        return false
    }

    public override fun initCustomerStatusManager() {

    }

    override fun setContentViewId(): Int {
        return R.layout.activity_orderdetails
    }

    override fun createPresenter() {
        mPresenter = POrderDetailsImpl(mContext, this)
    }

    override fun showLoading() {
        showDefaultDialog()
    }

    override fun hideLoading() {
        hideDefaultDialog()
    }

}
